
// import React from "react";
// // 导出一个默认函数，名为App
// export default function App() {
//   // 返回一个div元素，包含一个h1元素，一个p元素，和一个a元素
//   return (
//     <div className="App">
//       <h1>ADMIN-R</h1>
//     </div>
//   );
// }

import React, { useEffect, useState, createContext, useRef } from 'react';
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
// Modal 会被注入的代码所使用，请不要删除
import { ConfigProvider, Modal } from 'antd';
// import 'antd/dist/antd.less';
import _ from 'lodash';
// import HeaderMenu from './components/menu/SideMenu';
import Content from './routers';
import './App.less';


export interface ICommonState {
  sideMenuBgMode: string;
  setSideMenuBgMode: (color: string) => void;
}

// 可以匿名访问的路由 
const anonymousRoutes = ['/login'];
// 判断是否是匿名访问的路由
const anonymous = _.some(anonymousRoutes, (route) => location.pathname.startsWith(route));
// 初始化数据 context
export const CommonStateContext = createContext({} as ICommonState);

function App() {
  const initialized = useRef(false);
  const [commonState, setCommonState] = useState<ICommonState>({
    sideMenuBgMode: localStorage.getItem('sideMenuBgMode') || 'theme',
    setSideMenuBgMode: (mode: string) => {
      window.localStorage.setItem('sideMenuBgMode', mode);
      setCommonState((state) => ({ ...state, sideMenuBgMode: mode }));
    },
  });

  useEffect(() => {
    try {
      (async () => {
        const iconLink = document.querySelector("link[rel~='icon']") as any;
        let siteInfo;
        document.title = 'MY ADMIN';
        if (iconLink) {
          iconLink.href = siteInfo?.favicon_url || '/image/favicon-zt.ico';
        }
        // 非匿名访问，需要初始化一些公共数据
        if (!anonymous) {
          initialized.current = true;
          setCommonState((state) => {
            return {
              ...state,
         
            };
          });
        } 
      })();
    } catch (error) {
      console.error(error);
    }
  }, []);

  // 初始化中不渲染任何内容
  if (!initialized.current) {
    return null;
  }

  return (
    <div className='App'>
      <CommonStateContext.Provider value={commonState}>
        {/* @ts-ignore */}
          <Router>
            {/* @ts-ignore */}
            <Switch>
              <>
                <Content />
              </>
            </Switch>
          </Router>
      </CommonStateContext.Provider>
    </div>
  );
}

export default App;
